
let baseUrl = "http://localhost:8001/";

$.ajaxSettings.contentType = "application/json;charset=utf-8";
let loading = "";

function urlLink(str) {
    let url = {
        //测试连接
        test: {
            method: 'post',
            url: 'setting/test-connection'
        },
        // 显式连接信息
        show: {
            method: 'get',
            url: 'setting/show-connection'
        },
        // 保存连接信息
        save: {
            method: 'post',
            url: 'setting/save-connection'
        },
        // 获取任务种类
        taskType: {
            method: 'get',
            url: 'task-setting/task-type'
        },
        // sql展示
        showSql: {
            method: 'get',
            url: 'task-setting/showSql'
        },
        // 保存任务sql
        saveTask: {
            method: 'post',
            url: 'task-setting/save'
        },
        // 任务展示
        showTasks: {
            method: 'get',
            url: 'task/show-tasks'
        },
        // 开启/关闭定时任务
        switchTask: {
            method: 'get',
            url: 'task/switch-task'
        },
        // 手动执行一次任务
        exec: {
            method: 'get',
            url: 'task/exec'
        },
        showCredential: {
            method: 'get',
            url: 'credential/show-credential'
        },
        saveCredential: {
            method: 'post',
            url: 'credential/save-credential'
        }
    }

    return url[str];
}


function ajaxMethod(str, obj) {
    let link = urlLink(str);

    if (link.method == "post") {
        obj = JSON.stringify(obj);
    }

    let result = {};

    $.ajax({
        type: link.method,
        contentType: "application/json;charset=utf-8",
        url: baseUrl + link.url,
        data: obj,
        dataType: 'json',
        async: false,
        cache: true,
        beforeSend: function (request) {
            //显示
            //$('#loading').modal('show');
        },
        complete: function () {
            // 隐藏
            //$('#loading').modal('hide');
        },
        success: function (data) {
            result = data;
        },
        error: function (XMLHttpRequest) {
            console.log(XMLHttpRequest);
        }
    });

    return result;
}
